package com.edu.exam.entity.grading;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;
import com.edu.exam.enums.grading.GradingTaskStatus;
import com.edu.exam.entity.curriculum.classification.DifficultyLevel;
import com.edu.exam.enums.common.AssignmentStrategy;
import com.edu.exam.enums.common.TaskType;
import com.edu.exam.enums.common.WorkflowStatus;
import com.edu.exam.enums.grading.QualityCheckRequirement;
import com.edu.exam.enums.grading.TaskSource;

/**
 * 网上批阅任务实体类
 * [所属模块] E-04 扫描阅卷处理模块
 *
 * 根据E-04-05 网上批阅子模块设计实现
 * 对应grading_task数据表
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
public class OnlineGradingTask {

    @Id
    @Column(name = "task_id", length = 64)
    private String taskId;

    /**
     * 考试ID
     */
    @Column(name = "exam_id", nullable = false, length = 64)
    private String examId;

    /**
     * 教师ID
     */
    @Column(name = "teacher_id", nullable = false, length = 64)
    private String teacherId;

    /**
     * 任务类型：SUBJECTIVE（主观题批阅）、REVIEW（复核批阅）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "task_type", nullable = false, length = 32)
    private TaskType taskType;

    /**
     * 任务状态：PENDING（待处理）、ACTIVE（进行中）、COMPLETED（已完成）、CANCELLED（已取消）、PAUSED（暂停）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 32)
    private GradingTaskStatus status;

    /**
     * 总图像数
     */
    @Column(name = "total_images", nullable = false)
    private Integer totalImages;

    /**
     * 已完成图像数
     */
    @Column(name = "completed_images", nullable = false)
    private Integer completedImages;

    /**
     * 开始时间
     */
    @Column(name = "start_time")
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    @Column(name = "end_time")
    private LocalDateTime endTime;

    /**
     * 截止时间
     */
    @Column(name = "deadline")
    private LocalDateTime deadline;

    /**
     * 优先级（1-10，数字越大优先级越高）
     */
    @Column(name = "priority", nullable = false)
    private Integer priority;

    /**
     * 预计时长（分钟）
     */
    @Column(name = "estimated_duration")
    private Integer estimatedDuration;

    /**
     * 实际时长（分钟）
     */
    @Column(name = "actual_duration")
    private Integer actualDuration;

    /**
     * 质量评分
     */
    @Column(name = "quality_score", precision = 5, scale = 2)
    private Double qualityScore;

    /**
     * 任务描述
     */
    @Column(name = "description", length = 500)
    private String description;

    /**
     * 任务备注
     */
    @Column(name = "remarks", length = 1000, columnDefinition = "TEXT")
    private String remarks;

    /**
     * 任务标签（JSON格式，用于分类）
     */
    @Column(name = "task_tags", length = 500)
    private String taskTags;

    /**
     * 任务配置（JSON格式）
     */
    @Column(name = "task_config", length = 2000, columnDefinition = "TEXT")
    private String taskConfig;

    /**
     * 难度等级：EASY（简单）、MEDIUM（中等）、HARD（困难）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "difficulty_level", length = 16)
    private DifficultyLevel difficultyLevel;

    /**
     * 批阅标准ID
     */
    @Column(name = "grading_standard_id", length = 64)
    private String gradingStandardId;

    /**
     * 分配策略：AUTO（自动分配）、MANUAL（手动分配）、BALANCED（均衡分配）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "assignment_strategy", length = 32)
    private AssignmentStrategy assignmentStrategy;

    /**
     * 是否允许协作批阅
     */
    @Column(name = "allow_collaboration")
    private Boolean allowCollaboration;

    /**
     * 最大协作人数
     */
    @Column(name = "max_collaborators")
    private Integer maxCollaborators;

    /**
     * 质量检查要求：NONE（不需要）、OPTIONAL（可选）、RECOMMENDED（建议）、REQUIRED（必须）、MANDATORY（强制）、DOUBLE_CHECK（双重检查）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "quality_check_requirement", length = 32)
    private QualityCheckRequirement qualityCheckRequirement;

    /**
     * 任务来源：SYSTEM（系统生成）、MANUAL（手动创建）、IMPORT（导入生成）、BATCH（批量创建）、TEMPLATE（模板创建）、API（API创建）、COPY（复制生成）、AUTO_ASSIGN（自动分配）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "task_source", length = 32)
    private TaskSource taskSource;

    /**
     * 父任务ID（用于子任务管理）
     */
    @Column(name = "parent_task_id", length = 64)
    private String parentTaskId;

    /**
     * 批次ID（用于批量任务管理）
     */
    @Column(name = "batch_id", length = 64)
    private String batchId;

    /**
     * 重试次数
     */
    @Column(name = "retry_count")
    private Integer retryCount;

    /**
     * 最大重试次数
     */
    @Column(name = "max_retry_count")
    private Integer maxRetryCount;

    /**
     * 最后错误信息
     */
    @Column(name = "last_error_message", length = 1000, columnDefinition = "TEXT")
    private String lastErrorMessage;

    /**
     * 最后错误时间
     */
    @Column(name = "last_error_time")
    private LocalDateTime lastErrorTime;

    /**
     * 扩展属性（JSON格式）
     */
    @Column(name = "additional_attributes", length = 2000, columnDefinition = "TEXT")
    private String additionalAttributes;

    /**
     * 创建人
     */
    @Column(name = "created_by", length = 64)
    private String createdBy;

    /**
     * 更新人
     */
    @Column(name = "updated_by", length = 64)
    private String updatedBy;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 版本号（用于乐观锁）
     */
    @Version
    @Column(name = "version", nullable = false)
    private Integer version;


    /**
     * 关联的批阅会话列表
     */
    @OneToMany(mappedBy = "gradingTask", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<GradingSession> gradingSessions;

    /**
     * 关联的批阅图像列表
     */
    @OneToMany(mappedBy = "gradingTask", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<GradingImage> gradingImages;

    /**
     * 关联的质量检查列表
     */
    @OneToMany(mappedBy = "gradingTask", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<QualityCheck> qualityChecks;


}
